using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ICP_MS.Plugin.iMass;

public class FormUser : Form
{
	private IContainer components = null;

	private DataGridView dgvUser;

	private Button btnAdd;

	private Button btnDelete;

	private Button btnClose;

	private DataGridViewTextBoxColumn ColumnUser;

	private DataGridViewTextBoxColumn ColumnPsw;

	private DataGridViewTextBoxColumn ColumnAuthor;

	private Label lblTip;

	private Button btnPsw;

	public FormUser()
	{
		InitializeComponent();
	}

	private void FormUser_Load(object sender, EventArgs e)
	{
		Text = Lang.Txt("AddiMass.Text25");
		ColumnUser.HeaderText = Lang.Txt("AddiMass.Text26");
		ColumnPsw.HeaderText = Lang.Txt("AddiMass.Text27");
		ColumnAuthor.HeaderText = Lang.Txt("AddiMass.Text28");
		btnAdd.Text = Lang.Txt("AddiMass.Text29");
		btnDelete.Text = Lang.Txt("AddiMass.Text30");
		btnPsw.Text = Lang.Txt("AddiMass.Text27");
		btnClose.Text = Lang.Txt("AddiMass.Text31");
		LoadUser();
	}

	private void LoadUser()
	{
		dgvUser.Rows.Clear();
		string[] authors = new string[4]
		{
			Lang.Txt("AddiMass.Text1"),
			Lang.Txt("AddiMass.Text2"),
			Lang.Txt("AddiMass.Text23"),
			"ncs"
		};
		foreach (User user in UserManager.Users)
		{
			if (user.authority != 2 && user.authority != 3)
			{
				string name = user.name;
				string psw = user.psw;
				string author = authors[user.authority];
				dgvUser.Rows.Add(name, psw, author);
			}
		}
	}

	private void btnAdd_Click(object sender, EventArgs e)
	{
		FormAddUser dlg = new FormAddUser();
		dlg.ShowDialog();
		LoadUser();
	}

	private void btnDelete_Click(object sender, EventArgs e)
	{
		if (dgvUser.SelectedRows.Count != 0)
		{
			string user = dgvUser.SelectedRows[0].Cells[0].Value as string;
			if (!UserManager.Instance.DeleteUser(user))
			{
				lblTip.Text = Lang.Txt("AddiMass.Text24");
			}
			LoadUser();
		}
	}

	private void btnPsw_Click(object sender, EventArgs e)
	{
		if (dgvUser.SelectedRows.Count != 0)
		{
			string user = dgvUser.SelectedRows[0].Cells[0].Value as string;
			string psw = dgvUser.SelectedRows[0].Cells[1].Value as string;
			FormPsw dlg = new FormPsw(user, psw);
			dlg.ShowDialog();
			LoadUser();
		}
	}

	private void btnClose_Click(object sender, EventArgs e)
	{
		Close();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
		this.dgvUser = new System.Windows.Forms.DataGridView();
		this.ColumnUser = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColumnPsw = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColumnAuthor = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.btnAdd = new System.Windows.Forms.Button();
		this.btnDelete = new System.Windows.Forms.Button();
		this.btnClose = new System.Windows.Forms.Button();
		this.lblTip = new System.Windows.Forms.Label();
		this.btnPsw = new System.Windows.Forms.Button();
		((System.ComponentModel.ISupportInitialize)this.dgvUser).BeginInit();
		base.SuspendLayout();
		this.dgvUser.AllowUserToAddRows = false;
		this.dgvUser.AllowUserToDeleteRows = false;
		this.dgvUser.BackgroundColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dgvUser.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle3;
		this.dgvUser.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dgvUser.Columns.AddRange(this.ColumnUser, this.ColumnPsw, this.ColumnAuthor);
		this.dgvUser.Location = new System.Drawing.Point(12, 12);
		this.dgvUser.MultiSelect = false;
		this.dgvUser.Name = "dgvUser";
		dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		this.dgvUser.RowsDefaultCellStyle = dataGridViewCellStyle4;
		this.dgvUser.RowTemplate.Height = 23;
		this.dgvUser.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
		this.dgvUser.Size = new System.Drawing.Size(344, 184);
		this.dgvUser.TabIndex = 0;
		this.ColumnUser.HeaderText = "用户名";
		this.ColumnUser.Name = "ColumnUser";
		this.ColumnUser.ReadOnly = true;
		this.ColumnUser.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
		this.ColumnPsw.HeaderText = "密码";
		this.ColumnPsw.Name = "ColumnPsw";
		this.ColumnPsw.ReadOnly = true;
		this.ColumnPsw.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
		this.ColumnAuthor.HeaderText = "权限";
		this.ColumnAuthor.Name = "ColumnAuthor";
		this.ColumnAuthor.ReadOnly = true;
		this.ColumnAuthor.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
		this.btnAdd.Location = new System.Drawing.Point(13, 227);
		this.btnAdd.Name = "btnAdd";
		this.btnAdd.Size = new System.Drawing.Size(75, 23);
		this.btnAdd.TabIndex = 1;
		this.btnAdd.Text = "添加";
		this.btnAdd.UseVisualStyleBackColor = true;
		this.btnAdd.Click += new System.EventHandler(btnAdd_Click);
		this.btnDelete.Location = new System.Drawing.Point(100, 227);
		this.btnDelete.Name = "btnDelete";
		this.btnDelete.Size = new System.Drawing.Size(75, 23);
		this.btnDelete.TabIndex = 2;
		this.btnDelete.Text = "删除";
		this.btnDelete.UseVisualStyleBackColor = true;
		this.btnDelete.Click += new System.EventHandler(btnDelete_Click);
		this.btnClose.Location = new System.Drawing.Point(274, 226);
		this.btnClose.Name = "btnClose";
		this.btnClose.Size = new System.Drawing.Size(75, 23);
		this.btnClose.TabIndex = 3;
		this.btnClose.Text = "关闭";
		this.btnClose.UseVisualStyleBackColor = true;
		this.btnClose.Click += new System.EventHandler(btnClose_Click);
		this.lblTip.ForeColor = System.Drawing.Color.Red;
		this.lblTip.Location = new System.Drawing.Point(12, 200);
		this.lblTip.Name = "lblTip";
		this.lblTip.Size = new System.Drawing.Size(339, 23);
		this.lblTip.TabIndex = 4;
		this.lblTip.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
		this.btnPsw.Location = new System.Drawing.Point(187, 227);
		this.btnPsw.Name = "btnPsw";
		this.btnPsw.Size = new System.Drawing.Size(75, 23);
		this.btnPsw.TabIndex = 5;
		this.btnPsw.Text = "密码";
		this.btnPsw.UseVisualStyleBackColor = true;
		this.btnPsw.Click += new System.EventHandler(btnPsw_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(363, 262);
		base.Controls.Add(this.btnPsw);
		base.Controls.Add(this.lblTip);
		base.Controls.Add(this.btnClose);
		base.Controls.Add(this.btnDelete);
		base.Controls.Add(this.btnAdd);
		base.Controls.Add(this.dgvUser);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "FormUser";
		base.ShowIcon = false;
		this.Text = "用户管理";
		base.Load += new System.EventHandler(FormUser_Load);
		((System.ComponentModel.ISupportInitialize)this.dgvUser).EndInit();
		base.ResumeLayout(false);
	}
}
